package com.future.lock.me;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.future.baselib.utils.ToastUtils;
import com.future.baselib.view.CircleImageView;
import com.future.lock.R;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.common.http.response.DefaultHttpResponse;
import com.future.lock.me.activity.MachineManagerActivity;
import com.future.lock.me.activity.MyFavoriteActivity;
import com.future.lock.me.activity.UserInfoActivity;
import com.future.lock.me.entity.response.LoginResponse;
import com.future.lock.me.entity.response.OrderNumResponse;
import com.future.lock.me.order.OrderListActivity;
import com.future.lock.me.order.entity.bean.OrderListInfoBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import q.rorbin.badgeview.QBadgeView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 我的 Fragment
 */
public class PersonFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.iv_person)
    CircleImageView ivPerson;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.iv_dfk)
    ImageView ivDfk;
    @BindView(R.id.iv_dfh)
    ImageView ivDfh;
    @BindView(R.id.layout_order_wait_pay)
    View layoutDfk;
    @BindView(R.id.layout_order_wait_delivery)
    View layoutDfh;
    @BindView(R.id.layout_order_wait_reach)
    View layoutDsh;
    @BindView(R.id.layout_order_wait_pay_1)
    View layoutDfk1;
    @BindView(R.id.layout_order_wait_delivery_1)
    View layoutDfh1;
    @BindView(R.id.layout_order_wait_reach_1)
    View layoutDsh1;
    @BindView(R.id.iv_dsh)
    ImageView ivDsh;


    public PersonFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    public void initView() {
        setUpUserInfo();
        updateInfo();
        getServerPhone();
        getOrderNum();
    }

    private void getOrderNum() {
        HttpUtils.getHttpApi().getOrderNum(MyApp.getUserToken()).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                OrderNumResponse orderNumResponse = new OrderNumResponse();
                orderNumResponse.parse(response.body());
                if (orderNumResponse.success == 1) {
                    new QBadgeView(getContext())
                            .setShowShadow(false)
                            .setGravityOffset(20,5,true)
                            .bindTarget(layoutDfh)
                            .setBadgeNumber(orderNumResponse.num_dfh);
                    new QBadgeView(getContext()).setShowShadow(false)
                            .setGravityOffset(20,5,true)
                            .bindTarget(layoutDfk)
                            .setBadgeNumber(orderNumResponse.num_dfk);
                    new QBadgeView(getContext()).setShowShadow(false)
                            .setGravityOffset(20,5,true)
                            .bindTarget(layoutDsh)
                            .setBadgeNumber(orderNumResponse.num_dsh);
                } else {
                    Toast.makeText(getContext(), "订单数量获取失败:" + orderNumResponse.msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(getContext(), "订单数量获取失败:" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getServerPhone() {
        HttpUtils.getHttpApi().getCustomServicePhone().enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                DefaultHttpResponse defaultHttpResponse = new DefaultHttpResponse();
                defaultHttpResponse.parse(response.body());
                if (defaultHttpResponse.success == 1) {
                    tvPhone.setText(defaultHttpResponse.data);
                } else {
                    Toast.makeText(getContext(), "客服电话获取失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(getContext(), "客服电话获取失败:" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void updateInfo() {
        HttpUtils.getHttpApi().updateUserInfo(MyApp.getInstance().getUserToken()).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                LoginResponse loginResponse = new LoginResponse();
                loginResponse.parse(response.body());
                if (loginResponse.success == 1) {
                    MyApp.setUserToken(loginResponse.loginBean.user_token);
                    MyApp.setUserInfo(loginResponse.loginBean);
                    setUpUserInfo();
                } else {
                    if (loginResponse.code == 110) {
                        RefreshDataUtils.logout(getContext());
                    }
                    Toast.makeText(getContext(), loginResponse.msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setUpUserInfo() {
        if (MyApp.getUserInfo() != null) {
            tvName.setText(MyApp.getUserInfo().nick_name);
            Glide.with(this).load(MyApp.getUserInfo().head_imgurl)
                    .placeholder(R.mipmap.ic_person_default)
                    .dontAnimate()
                    .into(ivPerson);
        }
    }


    public void toOrder(int type) {
        Intent intent = new Intent(getContext(), OrderListActivity.class);
        intent.putExtra(OrderListActivity.ORDER_TYPE, type);
        startActivity(intent);
    }

    @OnClick({R.id.layout_order_wait_pay, R.id.layout_order_wait_delivery, R.id.layout_order_wait_reach
            , R.id.layout_order_all, R.id.layout_my_favorite, R.id.layout_machine_manager
            , R.id.layout_call_cs, R.id.tv_name, R.id.iv_person})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_order_wait_pay:
                toOrder(OrderListInfoBean.ORDER_STATUS_WAIT_TO_PAY);
                break;
            case R.id.layout_order_wait_delivery:
                toOrder(OrderListInfoBean.ORDER_STATUS_PAIED);
                break;
            case R.id.layout_order_wait_reach:
                toOrder(OrderListInfoBean.ORDER_STATUS_WAIT_FOR_DELIVERY);
                break;
            case R.id.layout_order_all:
                toOrder(OrderListInfoBean.ORDER_STATUS_NONE);
                break;
            case R.id.layout_my_favorite://我的收藏
                startActivity(new Intent(getContext(), MyFavoriteActivity.class));
                break;
            case R.id.layout_machine_manager://设备管理
                startActivity(new Intent(getContext(), MachineManagerActivity.class));
                break;
            case R.id.layout_call_cs://联系客服
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tvPhone.getText().toString().trim());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.tv_name:
            case R.id.iv_person:
                startActivityForResult(new Intent(getContext(), UserInfoActivity.class), 0x123);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x123) {
            updateInfo();
        }
    }
}
